package com.lianyu507.service.impl;

import com.lianyu507.mapper.ShUserWordDomainMapper;
import com.lianyu507.mapper.WordShowDomainMapper;

import com.lianyu507.pojo.MbUser;
import com.lianyu507.pojo.ShUserWordDetails;
import com.lianyu507.pojo.ShUserWordExample;
import com.lianyu507.pojo.*;
import com.lianyu507.service.CrWordTranslateService;
import com.lianyu507.service.ShUserWordService;
import com.lianyu507.service.WordShowDomainService;
import com.lianyu507.util.SystemStatus;
import com.lianyu507.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WordShowDomainServiceImpl implements WordShowDomainService {

    @Autowired
    private WordShowDomainMapper wordShowDomainMapper;

    @Autowired
    private ShUserWordService shUserWordService;

    @Autowired
    private CrWordTranslateService wordTranslateService;

    @Autowired
    private ShUserWordDomainMapper shUserWordMapper;

    /**
     * 通过用户id获取到 当前用户所有单词信息
     * @param userId
     * @return
     */
    @Override
    public List<CrWordTranslate> getAllWordByUserId(Integer userId) {

        ShUserWordExample userExample = new ShUserWordExample();
        userExample.or().andUserIdEqualTo(userId);

        List<ShUserWord> userWords = shUserWordService.selectByExample(userExample);

        ArrayList<Integer> ids = new ArrayList<>();
        for (ShUserWord userWord : userWords) {
            ids.add(userWord.getId());
        }
        List<CrWordTranslate> list = wordShowDomainMapper.findAllWordByWordId(ids);
        return list;
    }

    //根据单词id和单词获取一个单词翻译表的对象
    @Override
    public CrWordTranslate getCrWordTranslate(CrWordTranslate crWordTranslate) {
        //id和单词一个都不能为空

        if(crWordTranslate.getEnglish()==null){
            return crWordTranslate;
        }
        CrWordTranslate crWordTranslate1=wordShowDomainMapper.getCrWordTranslate(crWordTranslate.getEnglish().toLowerCase());

        return crWordTranslate1;
    }

    @Override
    public void addOrUpdateWordByshUserWordByWordId(ShUserWord shUserWord) {
        //添加或更新单词
        if (shUserWord==null){
            throw new RuntimeException("异常请求,单词不存在");
        }
        addOrUpdateNewWord(shUserWord);

    }
    @Override
    public void addOrUpdateWordByshUserWordByEnglish(ShUserWord shUserWord, String english) {
        //通过单词获取单词id
        CrWordTranslateExample cte = new CrWordTranslateExample();
        cte.or().andEnglishEqualTo(english);
        List<CrWordTranslate> crWordTranslates = wordTranslateService.selectByExample(cte);
        if(crWordTranslates.size()<=0){
            throw new RuntimeException("异常请求，单词不存在");
        }
        Integer wordId = crWordTranslates.get(0).getId();
        shUserWord.setWordId(wordId);
        //添加或更新单词
        addOrUpdateNewWord(shUserWord);
    }
    /**
     * 添加/更新用户单词公共方法
     */
    private void addOrUpdateNewWord(ShUserWord shUserWord){
        MbUser mbUser = UserContext.getCurrentUser();
        //判断用户是否登录
        if(mbUser==null){
            throw new RuntimeException("用户未登录，请登录");
        }
        //先判断是否有该单词，有添加，没有则更新
        ShUserWordExample shUserWordExample = new ShUserWordExample();
        shUserWordExample.or().andUserIdEqualTo(mbUser.getId()).andWordIdEqualTo(shUserWord.getWordId());
        List<ShUserWord> shUserWordList = this.shUserWordService.selectByExample(shUserWordExample);
        if (shUserWordList.size() > 0) {
            //判断用户是否移除生单词
            ShUserWord shUserWord1 = shUserWordList.get(0);

            if(shUserWord1.getStatus()== SystemStatus.SH_USER_WORD_REMOVER){
                shUserWord1.setStatus(SystemStatus.SH_USER_WORD_UNFAMILIAR);
            }
            //设置最近的文章
            shUserWord1.setArticleId(shUserWord.getArticleId());
            shUserWord1.setUpdateTime(new Date());
            shUserWord1.setCount(shUserWord.getCount() + 1);

            this.shUserWordService.updateByPrimaryKeySelective(shUserWord1);
        } else {
            //插入用户生单词
            shUserWord.setCount(1);
            shUserWord.setUserId(mbUser.getId());
            Date date = new Date();
            shUserWord.setCreateTime(date);
            shUserWord.setUpdateTime(date);
            shUserWord.setStatus(SystemStatus.SH_USER_WORD_UNFAMILIAR);
            this.shUserWordService.insertSelective(shUserWord);
        }
    }


    @Override
    public List<ShUserWord> selectWordByIsRemeber(Integer userId, byte status) {

        ShUserWordExample shUserWordExample = new ShUserWordExample();
        shUserWordExample.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(status);
        List<ShUserWord> shUserWords = shUserWordService.selectByExample(shUserWordExample);
        if (shUserWords == null) {
            if (status == SystemStatus.SH_USER_WORD_UNFAMILIAR)
                throw new RuntimeException("用户没有未记住的单词");
            if (status == SystemStatus.SH_USER_WORD_REMEMER)
                throw new RuntimeException("用户没有已记住单词");
        }
        return shUserWords;
    }

    @Override
    public List<ShUserWordDetails> queryWordsAndCountByUserId(Integer userId) {
        return shUserWordMapper.queryWordsAndCountByUserId(userId);
    }

    @Override
    public List<ShUserWordDetails> queryWordsAndCount() {
        return shUserWordMapper.queryWordsAndCount();
    }

}
